package com.tanhua.dubbo.api.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collections;
import java.util.List;

@Service
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;


    public Page selectPage(Integer pageNum, Integer pagesize, List<Long> friendIds ,String keyword) {
        Page<UserInfo> page = new Page<>(pageNum,pagesize);
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        if(friendIds!=null&&friendIds.size()>0){
            wrapper.in("id",friendIds);
        }
        if(StringUtils.isEmpty(keyword)){
            wrapper.like("nickName",keyword);
        }
        userInfoMapper.selectPage(page, wrapper);
        return page;
    }


    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新用户的信息
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }


    //根据id查找用户信息对象
    @Override
    public UserInfo findById(Long id) {
        return userInfoMapper.selectById(id);
    }

    //查询所有的用户
    @Override
    public Page findByPage(Integer pageNum, Integer pagesize) {
        Page<UserInfo> page = new Page(pageNum,pagesize);
        userInfoMapper.selectPage(page,null);
        return page;
    }

    @Override
    public List<UserInfo> userInfos(List<Long> userIds) {
        return userInfoMapper.selectBatchIds(userIds);
    }
}
